package com.feidee.fd.sml.algorithm.ml

import com.feidee.fd.sml.algorithm.component.ml.classification.{LinearSVCComponent, LinearSVCParam}
import com.feidee.fd.sml.algorithm.util.{TestingDataGenerator, ToolClass}
import org.scalatest.FunSuite

/**
  * @author YongChen
  * @date 2018/12/25 10:18
  * @description 算法规则函数
  * @reviewer dongguosheng
  */
class LinearSVCSuite extends FunSuite {
  val lsc = new LinearSVCComponent
  val paramStr: String =
    """
      |{
      |	'input_pt': 'a',
      |	'output_pt': 'b',
      | 'hive_table': '',
      | 'flow_time': '',
      |	'featuresCol': 'features',
      |	'labelCol': 'label',
//      | 'predictionCol': 'pro',
      |	'modelPath': '',
      |	'metrics': ['accuracy', 'auc', 'pr', 'abc'] ,
      | 'regParam': 0.1 ,
      | 'maxIter': 10 ,
      | 'fitIntercept': true ,
      | 'tol': 1E-6 ,
      | 'standardization': true ,
//      |  weightCol: String, //  param weightCol的值
      |  'threshold' : 0.0 ,
      |  'aggregationDepth' : 2
      }
    """.stripMargin
  val param: LinearSVCParam = lsc.parseParam(new ToolClass().encrypt(paramStr))


  /**
    * MultilayerPerceptron 参数构造方法测试：检测到读取出合法参数 & 成功赋值默认参数，即认为测试通过
    */
  test("LinearSVC parameter construction test") {
    assert(
      "a".equals(param.input_pt)
        & "b".equals(param.output_pt)
        & "".equals(param.hive_table)
        & "".equals(param.flow_time)
        & "features".equals(param.featuresCol)
        & "label".equals(param.labelCol)
//        & "pro".equals(param.predictionCol)
        & "".equals(param.modelPath)
        & param.metrics.sameElements(Array("accuracy", "auc", "pr", "abc"))
        & param.regParam==0.1
        & param.maxIter == 10
        & param.fitIntercept
        & param.tol == 1E-6
        & param.standardization
        & param.threshold == 0.0
        & param.aggregationDepth==2
    )
  }


  /**
    * GBDT 模型训练功能测试：传入生成的测试数据和正确参数，可以正常训练 LinearSVC 模型，且预测结果列等于生成数据列数，即认为测试通过
    */
  test("LinearSVC training function test") {

    val testingData = TestingDataGenerator.sampleBinaryClassificationData

    val splits = testingData.randomSplit(Array(0.6, 0.4))
    val train = splits(0)
    val test = splits(1)

    val model = lsc.train(param, train)

    val result = model.transform(test)
    result.show()
  }

}
